package com.bluebench.bigul.service.processor;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import com.bluebench.bigul.domain.Element;
import com.bluebench.bigul.domain.Element.Parameter;
import com.bluebench.bigul.domain.Invocation;
import com.bluebench.bigul.domain.Tag;
import com.bluebench.bigul.exception.GraphicInitializationException;
import com.bluebench.bigul.service.TypeFactory;


public class ParameterProcessor extends GenericProcessor{
	
	private Tag tag;
	
	@Override
	public Object process() throws SecurityException, IllegalArgumentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException, GraphicInitializationException {
	
		Parameter parameter = (Parameter)tag;
		boolean isNested = parameter.isNested();
		Object value = parameter.getValue();		
		Object returnObj = null;
		if(value instanceof Element){			
			Element e = (Element)value;
			e.setProcessor(GenericProcessor.getInstance(ELEMENT_PROCESSOR));
			returnObj = e.process();			
		}else if(value instanceof ArrayList){
			CollectionProcessor processor = (CollectionProcessor)GenericProcessor.getInstance(COLLECTION_PROCESSOR);
			processor.set(tag);
			processor.setCollection(value);
			returnObj = processor.process();			
		}else{
			if(parameter.isFormatted()){
				ParagraphPartProcessor processor = (ParagraphPartProcessor)GenericProcessor.getInstance(PARAGRAPH_PART_PROCESSOR);
				processor.set(value.toString());
				returnObj = processor.process();
			}else{
				returnObj = value;
			}
		}
		return returnObj;
	}
	
	@Override
	public void set(Tag t) {
		tag = t;
	}

}
